package com.stateflow.facade.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stateflow.facade.common.model.res.CommonResult;
import com.stateflow.facade.common.util.CommonUtils;
import com.stateflow.facade.common.util.HttpUtils;
import com.stateflow.facade.common.util.JsonUtils;
import com.stateflow.facade.common.util.StringUtils;
import com.stateflow.facade.dto.OperatorDTO;
import com.stateflow.facade.dto.req.GetOperatorListRequestDTO;
import com.stateflow.facade.dto.req.StateFlowRequestDTO;
import com.stateflow.facade.dto.notify.StateFlowNotifyDTO;

import java.util.List;

/**
 * Created by weiqingming on 2019/12/30.
 * 请求状态流转服务工具类
 */
public class RequestStateFlowUtils {

    private static final String STATE_FLOW_PATH = "/stateflow/stateFlow.json";
    private static final String GET_OPERATOR_LIST_PATH = "/stateflow/getOperatorList.json";

    /** 完整的请求状态流转地址 */
    private String completeStateFLowAddress = "";
    /** 完整的获取状态下操作列表地址 */
    private String completeGetOperatorListAddress = "";

    /** 部署的地址头 */
    private String headAddress = "";
    /** 账户KEY */
    private String accessKey = "";
    /** 账户SECRET */
    private String accessSecret = "";

    public RequestStateFlowUtils(String headAddress, String accessKey, String accessSecret){
        this.headAddress = headAddress;
        this.accessKey = accessKey;
        this.accessSecret = accessSecret;
    }


    /**
     * 状态流转
     * @param req
     * @return
     */
    public CommonResult<StateFlowNotifyDTO> stateFlow(StateFlowRequestDTO req){
        CommonResult<StateFlowNotifyDTO> result = null;
        if (req == null) return null;

        //设置签名
        req.setSign(CommonUtils.md5(StringUtils.compose(accessKey, accessSecret, req.getOrderNumber(), req.getOperatorCode())));

        //组装地址
        String url = completeStateFLowAddress;
        if (!StringUtils.isNotBlank(completeStateFLowAddress)){
            url = headAddress + STATE_FLOW_PATH;
        }

        //发送请求
        String resultJson = HttpUtils.sendHttpPost(url,req.toString());
        if (StringUtils.isNotBlank(resultJson)){
            result = JsonUtils.toObject(resultJson, CommonResult.class);
            if (result.getSuccess() && result.getData() instanceof JSONObject) {
                result.setData(JsonUtils.toObject(JsonUtils.toJson(result.getData()),StateFlowNotifyDTO.class));
                return result;
            }
        }

        return result;
    }


    /**
     * 获取状态下的操作列表
     * @param req
     * @return
     */
    public CommonResult<List<OperatorDTO>> getOperatorList(GetOperatorListRequestDTO req){
        CommonResult<List<OperatorDTO>> result = null;
        if (req == null) return null;

        //设置签名
        req.setSign(CommonUtils.md5(StringUtils.compose(accessKey, accessSecret, req.getConfigId(), req.getState())));

        //组装地址
        String url = completeGetOperatorListAddress;
        if (!StringUtils.isNotBlank(completeGetOperatorListAddress)){
            url = headAddress + GET_OPERATOR_LIST_PATH;
        }

        //发送请求
        String resultJson = HttpUtils.sendHttpPost(url, req.toString());
        if (StringUtils.isNotBlank(resultJson)) {
            result = JsonUtils.toObject(resultJson, CommonResult.class);
            if (result.getSuccess() && result.getData() instanceof JSONArray) {
                result.setData(JSON.parseArray(JsonUtils.toJson(result.getData()), OperatorDTO.class));
                return result;
            }
        }

        return result;
    }
}
